package com.semlab.server.enrichment;

import com.semlab.server.domain.Movie;
import com.semlab.server.graph.ConceptRepository;
import com.semlab.server.graph.Neo4jTemplate;
import com.semlab.server.resources.Concept;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Slobodan
 */
@Component
public class EnrichService {
    
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    DBpediaExtractor dbpedia;

    @Autowired
    ConceptRepository concepts;

    @Autowired
    DataMiner miner;

    @Autowired
    Neo4jTemplate template;

    public Concept enrichConcept(Concept concept) {
        template.beginTransaction();

        // ask nemanja for concepts
        Concept dbconcept = dbpedia.enrichConceptByProperty(concept);

        if (dbconcept != null) {
            log.debug("finished enrichment {}", concept);
            dbconcept.changeDateEnriched(true);
            dbconcept.setEnriched(true);
            concepts.persist(dbconcept);
            template.finishTransaction();
            return dbconcept;
        }else{
            log.error("concept could not be enriched");
            concept.setEnriched(true);
            concepts.persist(concept);
            return concept;
        }
//        }
    }
    
    public Movie enrichMovie(Movie movie){
        Concept enriched = enrichConcept(concepts.getById(movie.getConceptId()));
        return  miner.parseMovieInDetail(enriched);
    }

}
